<script setup>

import ChatKnowledgeDocumentBase from "@/components/knowledge/struct/chat-knowledge-document-base.vue";
import {defineEmits, ref} from "vue";
import {saveOrUpdateText} from "@/api/http/chat-knowledge.js";
import {userInfo} from "@/stores/userStore.js";
import {ElMessage} from "element-plus";


const formVal = ref({});
const dialogShow = ref(false);
const saveBtnLoading = ref(false);

const saveFunc = () => {
  saveBtnLoading.value = true;
  saveOrUpdateText({
    "userId": userInfo().userId,
    ...formVal.value
  }).then(res => {
    saveBtnLoading.value = false;
    emit('close');
    emit('refreshList');

    ElMessage({
      type: "success",
      message: "保存成功"
    })
  }).catch(res => {
    saveBtnLoading.value = false;
  })
}

const cancelFunc = () => {
  if (formVal.value.title === undefined
      &&
      formVal.value.content === undefined
  ) {
    emit('close');
  } else {
    dialogShow.value = true;
  }
}

const emit = defineEmits(['close', 'refreshList'])
</script>
<template>

  <chat-knowledge-document-base
      v-model:form-val="formVal"
  >
    <template #footer>
      <el-button-group style="float: right">
        <el-button
            type="primary"
            @click="saveFunc"
        >保存
        </el-button>
        <el-button
            @click="cancelFunc"
        >关闭
        </el-button>
      </el-button-group>
    </template>

  </chat-knowledge-document-base>
  <el-dialog
      v-model="dialogShow"
  >
    确认清空并退出？
    <template #footer>
      <el-button @click="emit('close')">确认</el-button>
      <el-button @click="dialogShow=false">取消</el-button>
    </template>
  </el-dialog>

</template>

<style scoped lang="scss">

</style>